from odoo.exceptions import UserError
from ..static.error_message_json import response_json, request_json, business_json

error_type = ['request', 'business', 'response']  # 错误类型
version_string = 'v1'  # 版本号
content_type = 'application/json; charset=utf-8'  # 文本类型


class RPInterfaceModelForResponse(object):
    """
    接口模型  - Response
    该模型用于输出接口的响应数据

    - code: 响应代码
    - error_type: 保存错误类型 {'request':'请求错误','business':'业务错误'}
    - message_code: 消息代码
    - message: { 'request':'请求返回信息',(error_type = request使用)
                 'response':'请求返回信息',(error_type = response使用)
                 'business':'业务返回信息',(error_type = business使用)}
    """

    @classmethod
    def full_message(cls, error, message, message_code=-1):
        """填充消息类型"""
        message_dict = {'request': '', 'business': '', 'response': ''}  # 消息类型
        if error == 'request':
            message_dict['request'] = request_json.get(message_code, message) if message_code != -1 else message
        elif error == 'business':
            message_dict['business'] = business_json.get(message_code, message) if message_code != -1 else message
        elif error == 'response':
            message_dict['response'] = response_json.get(message_code, message) if message_code != -1 else message
        return message_dict

    def __init__(self, code=200, error='', message_code=-1, message=''):
        if all([error not in error_type, error != '']):
            raise UserError('不存在该错误类型')
        if all([error in error_type, message_code == -1, message == '']):
            raise UserError('存在error,但没有提供message或message_code')
        self.code = code
        self.error_type = error
        self.message_code = message_code
        self.message = self.full_message(error, message, message_code)

    def printf(self):
        return {'code': self.code,
                'error_type': self.error_type,
                'message_code': self.message_code,
                'message': self.message}


class RPInterfaceModelForGet(object):
    """
    接口模型  - Get
    该模型用于处理请求的通用数据

    - page: 页码
    - limit: 一页中的限制
    - __offset: 起始页

    """
    schema_json = {'type': 'object',
                   'properties': {'page': {'type': 'integer', },
                                  'limit': {'type': 'integer', }}}

    def __init__(self, page=1, limit=10):
        if any([not isinstance(page, int), not isinstance(limit, int)]):
            raise ValueError('page必须为整型，limit必须为整型')
        if any([page < 1, limit < 1]):
            raise ValueError('page不能小于1，limit不能小于1')
        # self.language = language
        self.page = page
        self.limit = limit
        self.__offset = (page - 1) * limit - 1 if page > 1 else 0

    @property
    def offset(self):
        return self.__offset

    @offset.setter
    def offset(self, value):
        self.__offset = value


class RPInterfaceModelForDELETE(object):
    """
    接口模型  - Delete
    该模型用于处理请求的通用数据

    - ids: odoo.model (id值)

    """
    schema_json = {'type': 'object',
                   'properties': {'ids': {'type': 'array',
                                          'items': {'type': 'integer'}}}}


class RPInterfaceModelForHeader(object):
    """
    接口模型  - 响应Header
    该模型用于输出接口的响应头

    - content_type: application/json; charset=utf-8
    - content_length: 响应文件长度
    - access_control_allow_origin: 处理代理
    - Access-Control-Allow-Methods: 允许的请求方式

     - version: 版本号
    # - user_class: 用户级别
    # - language: 语言，两位字母
    # - application: 应用端 {'web':'网页','screen':'大屏','merchant':'商家'}
    # - location: {'longitude':'','latitude':''}
    """

    def __init__(self, cl: int, ct=content_type, acao='*', version=version_string):
        self.cl = cl
        self.ct = ct
        self.acao = acao
        self.version = version

    def printf(self):
        return {'Content-Type': self.ct,
                'Content-Length': self.cl,
                'Access-Control-Allow-Origin': self.acao,
                'version': self.version,
                'Access-Control-Allow-Methods': ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS']}
